﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using IDAL;
using DALFactory;

namespace BLL
{
    /*公共方法实现*/
    public abstract class BaseService<T> where T:class,new()
    {
        /// <summary>
        /// 子类通过此方法获取到数据操作类
        /// </summary>
        public IDALsession DBsession
        {
            get { return new DALsession(); }
        }
        /// <summary>
        /// 弱性的初始化子类对象
        /// </summary>
        public abstract void setBaseDal();
        public IBaseDAL<T> Dal { get; set; }
        public BaseService()
        {
            setBaseDal();
        }

        public IQueryable<T> LoadData(Expression<Func<T, bool>> whereLambda) {
            return Dal.LoadData(whereLambda);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="c">排序类型</typeparam>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="totalCount">总页数</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderbyLambda">排序条件</param>
        /// <param name="isAsc">是否倒序</param>
        /// <returns></returns>

        public IQueryable<T> Paging<c>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, c>> orderbyLambda, bool isAsc)
        {
            return Dal.Paging<c>(pageIndex, pageSize,out totalCount, whereLambda, orderbyLambda, isAsc);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity">实体模型</param>
        public bool DeleteSign(T entity)
        {
            Dal.DeleteSign(entity);
            return DBsession.SaveChanges();
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity">实体模型</param>
        public bool EditSign(T entity)
        {
            Dal.EditSign(entity);
            return DBsession.SaveChanges();

        }
        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="entity">实体模型</param>
        /// <returns>返回插入成功后的实体模型</returns>
        public T AddSing(T entity)
        {
           Dal.AddSing(entity);
           DBsession.SaveChanges();
           return entity;
        }
    }
}
